﻿using System;
using System.Collections.Generic;
using System.Linq;
using Icms.Core.Model;
using Icms.Core.Repository;

namespace Icms.Service
{
    public class StatusesService : CrudService<Status>
    {
        public StatusesService(IRepo<Status> repo)
            : base(repo)
        {
        }

        public IEnumerable<Status> GetByCase(Guid caseId)
        {
            return repo.Where(o => o.CaseId == caseId).OrderBy(o => o.DateFrom);
        }

        public Status GetLastCaseStatus(Guid caseId)
        {
            return repo.Where(o => o.CaseId == caseId && o.DateTo == null).First();
        }

        public void StatusChange(Guid caseId, string statusCode, DateTime datefrom)
        {
            var cs = new Status
                         {
                             CaseId = caseId,
                             DateFrom = datefrom,
                             Code = statusCode
                         };

            var curstatus = GetLastCaseStatus(caseId);
            curstatus.DateTo = datefrom;
            repo.Insert(cs);
            repo.Save();
        }

    }
}